<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Code Analysis</title>
    <style>
        body {
            font-family: 'Franklin Gothic Medium';
            margin: 20px;
            background-color: #c6c6c6;
        }
        h1 {
            color: #3c3c3b;
            /* color: #f9f9f9; */
        }
        textarea {
            width: 50%;
            height: 200px;
            margin-bottom: 10px;
            /* background-color: #878787; */
            background-color: #c6c6c6;
            /* color: #f9f9f9; */
            color: #3c3c3b;
            /* border-color: #4dc8db; */
            border-color: #3c3c3b;
            border-radius: 10px;
            border-width: 1.5pt;
        }
        textarea:hover {
            /* border-color: #f9f9f9; */
            border-color: #4dc8db;
            transition: 0.4s;
        }
        textarea:focus {
            border-color: #f9f9f9;
            transition: 0.4s;
        }
        ::placeholder {
            /* color: #f9f9f9; */
            color: #3c3c3b;
        }
        button {
            /* padding: 10px 20px; */
            width: 10%;
            height: 40px;
            background-color: #4dc8db;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }
        button:hover {
            background-color: #2499ab;
        }
        #result {
            display: flex; 
            justify-content: center; 
            align-items: center;

        }
        #status {
            margin-top: 10px;
            font-weight: bold;
        }
        #cpg_info {
            margin-top: 10px;
            width: 33%;
            height: 33%;
            display: block;
            font-weight: bold;
            text-align: left;
            font-size: 16px;
            
        }
        #icon {
            margin-top: 10px;
            width: 33%;
            height: 33%;
            display: block;
            font-weight: bold;
            text-align: center;
            font-size: 24px;
        }
        #prediction {
            margin-top: 10px;
            width: 33%;
            height: 33%;
            display: block;
            font-weight: bold;
            text-align: right;
            font-size: 16px;
        }
        #analysis {
            width: 20%;
            height: 20%;
            position: relative;
            display: block;
            margin-left: auto;
            margin-right: auto;
            padding-bottom: 10px;
        }
    </style>
    <script>
        let processingDots = 0;

        function analyzeCode() {
            const code = document.getElementById('code').value;
            document.getElementById('status').innerText = 'Submitting code for analysis...';
            fetch('/analyze', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'code=' + encodeURIComponent(code)
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'Processing...') {
                    document.getElementById('status').innerText = 'Processing';
                    processingDots = 0;
                    pollForResult();
                }
            });
        }

        function pollForResult() {
            fetch('/result')
            .then(response => response.json())
            .then(data => {
                if (data.status === 'Processing...') {
                    processingDots = (processingDots + 1) % 4;
                    let dots = '.'.repeat(processingDots);
                    document.getElementById('status').innerText = 'Processing' + dots;
                    setTimeout(pollForResult, 1000);
                } else {
                    document.getElementById('status').innerText = 'Analysis complete.';
                    if (data.predicted_label == 0){
                        document.getElementById('icon').innerHTML = '<center><img src="icons/secure.png" alt="secure" id="icon"></center><br>The function is secure';
                    }
                    else if (data.predicted_label == 1){
                        document.getElementById('icon').innerHTML = '<center><img src="icons/unsecure.png" alt="unsecure" id="icon"></center><br>The function is vulnerable';
                    }
                    document.getElementById('cpg_info').innerHTML = data.n_nodes + ' nodes and ' + data.n_edges + ' edges in the CPG';
                    pred_info = 'Probability: ' + data.prediction_probability + 's' 
                    + '<br>Time to generate CPG: ' + data.time_to_generate_cpg + 's' 
                    + '<br>Time to embed: ' + data.time_to_embed + 's' 
                    + '<br>Time to inference: ' + data.time_to_inference + 's';
                    document.getElementById('prediction').innerHTML = pred_info;
                }
            });
        }
    </script>
</head>
<body>
    <center><h1>Source Code Vulnerability Detection</h1></center>
    <img src="icons/analysis.png" alt="analysis" id="analysis">
    <form onsubmit="event.preventDefault(); analyzeCode();">
        <center><textarea id="code" name="code" placeholder="Enter the code to analyze here..."></textarea><br></center>
        <center><button type="submit">Analyze</button></center>
    </form>
    <center><div id="status"></div></center>
    <div id="result">
        <!-- <div id="cpg_info">777 nodes,  77 edges </div>
        <div id="icon"><center><img src="icons/unsecure.png" alt="unsecure" id="icon"></center><br>The function is vulnerable</div>
        <div id="prediction"><br>Time to generate CPG: 0000<br>Time to generate CPG: 0000<br>Time to generate CPG: <br>Time to generate CPG: </div> -->
        <div id="cpg_info"></div>
        <div id="icon"></div>
        <div id="prediction"></div>
    </div>
</html>